

import 'package:bot_toast/bot_toast.dart';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:yun/entity/current_task_entity.dart';
import 'package:yun/page/index_page.dart';
import 'package:yun/tts/tts_helper.dart';

import 'entity/setting_entity.dart';
import 'log/logger.dart';
import 'mqtt/mqtt_helper.dart';
import 'serial/serial_port_helper.dart';


void main() async {
  await GetStorage.init();
  eventFactory = EventBus();
  box = GetStorage();
  if(box.hasData('setting_entity')){
    settingEntity = SettingEntity.fromJson(box.read("setting_entity"));
  }else{
    settingEntity = SettingEntity();
  }

  try{
    hfSerialPortHelper = SerialPortHelper(settingEntity.hfSerial??'/dev/ttyS2',115200);

  }catch(e){
    print(e);
  }


  mqttHelper = MqttHelper("sw.hwmao.cn",settingEntity.deviceNo??'HF01010101',1882);
  FlutterError.onError = (FlutterErrorDetails details) async {
    Logger().write('${details.exception}\r\n${details.stack}\r\n');
  };

  currentTask = CurrentTaskEntity();
  tts = TtsHelper()..initTTS();

  // videoPlayerHelper = VideoPlayerHelper();
  // videoPlayerHelper.setVolume(settingEntity.tipVolume??1);
  runApp(const MyApp());

}

late TtsHelper tts;
late SerialPortHelper hfSerialPortHelper;
late EventBus eventFactory;
late SettingEntity settingEntity;
late GetStorage box;
late MqttHelper mqttHelper;
late CurrentTaskEntity currentTask;


class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return  ScreenUtilInit(
      designSize: const Size(360, 640),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (_ , child) {
        return  GetMaterialApp(
            debugShowCheckedModeBanner: false,
            // theme: ThemeData(
            //   fontFamily: 'simsun',
            // ),
            builder: BotToastInit(),
            home: const Index()
        );
      },
    );
  }
}
